﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BoardController.Helper
{
    public class Msg
    {
        public MsgState curState = new MsgState();

        public byte[] Head = new byte[3];
        public byte Device;
        public byte Function;
        public byte[] Register = new byte[2];
        public byte[] RegCount = new byte[2];
        public List<byte> Data = new List<byte>();
        public byte[] Label = new byte[2];
        public byte[] CRC = new byte[2];

        public List<byte> GetList()
        {
            List<byte> temp = new List<byte>();
            foreach (var b in Head)
            {
                temp.Add(b);
            }
            temp.Add(Device);
            temp.Add(Function);
            foreach (var b in Register)
            {
                temp.Add(b);
            }
            foreach (var b in RegCount)
            {
                temp.Add(b);
            }
            foreach (var b in Data)
            {
                temp.Add(b);
            }
            foreach (var b in Label)
            {
                temp.Add(b);
            }

            return temp;
        }

    }

    public class MsgState
    {
        public StateInfo Name;
        public int Count;

        public MsgState()
        {
            Name = StateInfo.HEAD;
            Count = 3;
        }

        public void Next(int count = 0)
        {
            if (Count > 0)
            {
                return;
            }
            switch (Name)
            {
                case StateInfo.HEAD:
                    Name = StateInfo.DEVICE;
                    Count = 1;
                    break;
                case StateInfo.DEVICE:
                    Name = StateInfo.FUNCTION;
                    Count = 1;
                    break;
                case StateInfo.FUNCTION:
                    Name = StateInfo.REGISTER;
                    Count = 2;
                    break;
                case StateInfo.REGISTER:
                    Name = StateInfo.REGCOUNT;
                    Count = 2;
                    break;
                case StateInfo.REGCOUNT:
                    Name = StateInfo.DATA;
                    Count = count;
                    break;
                case StateInfo.DATA:
                    Name = StateInfo.LABEL;
                    Count = 2;
                    break;
                case StateInfo.LABEL:
                    Name = StateInfo.CRC;
                    Count = 2;
                    break;
                default:
                    //Console.WriteLine("Warning message");
                    break;
            }
        }

    }

    public enum StateInfo
    {
        HEAD,
        DEVICE,
        FUNCTION,
        REGISTER,
        REGCOUNT,
        DATA,
        LABEL,
        CRC
    }

    public class IMUTime
    {
        public int Day;
        public int Hour;
        public int Minute;
        public int Second;
        public int Milli;

        public double Difference(IMUTime new_time)
        {
            double diff = 0;
            diff += (new_time.Day - Day) * 24 * 60 * 60;
            diff += (new_time.Hour - Hour) * 60 * 60;
            diff += (new_time.Minute - Minute) * 60;
            diff += new_time.Second - Second;
            diff += (double)(new_time.Milli - Milli) / 1000;
            return diff;
        }

    }
}
